package com.pls.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.pls.mapper.*;

public class DataHelper<T> {
	private T t;

	public void set(T t) {
		this.t = t;
	}

	public T get() {
		return t;
	}

	//Returns Generic List using a Mapper Interface as parameter
	public List<T> ExecuteQuery(String query, Mapper<T> mapper) {
		Connection conn = null;
		PreparedStatement prepStatement = null;
		ConnectionManager connMan = new ConnectionManager();

		try {
			// open connection
			conn = connMan.getConnection(ConnectionStrings.getDb(),
					ConnectionStrings.getUser(),
					ConnectionStrings.getPassword(),
					ConnectionStrings.getDriver());

			//Prepare Statement query
			prepStatement = conn.prepareStatement(query);
			
			//Execute the result set
			ResultSet rs = prepStatement.executeQuery();

			//Maps the list to Java object
			return mapper.MapList(rs);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			//Close connection
			try {
				if (prepStatement != null) {
					prepStatement.close();
				}

				if (conn != null) {
					conn.close();
				}

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return null;
	}

	//Returns a Generic Type using a Mapper Interface as parameter
	public T ExecuteQuerySingle(String query, Mapper<T> mapper) {
		Connection conn = null;
		PreparedStatement prepStatement = null;
		ConnectionManager connMan = new ConnectionManager();

		try {
			conn = connMan.getConnection(ConnectionStrings.getDb(),
					ConnectionStrings.getUser(),
					ConnectionStrings.getPassword(),
					ConnectionStrings.getDriver());

			prepStatement = conn.prepareStatement(query);
			ResultSet rs = prepStatement.executeQuery();

			return mapper.Map(rs);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		finally {
			try {
				if (prepStatement != null) {
					prepStatement.close();
				}

				if (conn != null) {
					conn.close();
				}

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return null;
	}

	//Use for update scripts
	public int ExecuteUpdate(String query) {
		Connection conn = null;
		PreparedStatement prepStatement = null;
		ConnectionManager connMan = new ConnectionManager();
		int rowUpdateCount = 0;

		try {
			conn = connMan.getConnection(ConnectionStrings.getDb(),
					ConnectionStrings.getUser(),
					ConnectionStrings.getPassword(),
					ConnectionStrings.getDriver());

			prepStatement = conn.prepareStatement(query);
			rowUpdateCount = prepStatement.executeUpdate();

			return rowUpdateCount;

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		finally{
			try {
				if (prepStatement != null) {
					prepStatement.close();
				}

				if (conn != null) {
					conn.close();
				}

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return rowUpdateCount;
	}

	
	public int ExecuteUpdate(String query) {
		Connection conn = null;
		PreparedStatement prepStatement = null;
		ConnectionManager connMan = new ConnectionManager();
		int rowUpdateCount = 0;

		try {
			conn = connMan.getConnection(ConnectionStrings.getDb(),
					ConnectionStrings.getUser(),
					ConnectionStrings.getPassword(),
					ConnectionStrings.getDriver());
			System.out.println(conn);
			System.out.println(ConnectionStrings.getDb());
			prepStatement = conn.prepareStatement(query);
			rowUpdateCount = prepStatement.executeUpdate();

			return rowUpdateCount;

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		finally{
			try {
				if (prepStatement != null) {
					prepStatement.close();
				}

				if (conn != null) {
					conn.close();
				}

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return rowUpdateCount;
	}
}
